<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userFrom" :model="user" :rules="userRules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item prop="username" label="姓名:">
                <el-input v-model="user.username" style="width:300px" />
              </el-form-item>
              <el-form-item prop="password" label="密码:">
                <el-input v-model="user.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetail } from '@/api/user'
import { eidtEmployee } from '@/api/employees'
import JobInfo from './components/job-info'
import UserInfo from './components/user-info'
export default {
  components: { UserInfo, JobInfo },
  data() {
    return {
      userId: this.$route.params.id,
      user: {
        username: '',
        password: ''
      },
      // 校验表单规则
      userRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 因为后端回显的数据经过加密的，无法逆向得到原密码，所以用空格表示密码
    this.user = { ...await getUserDetail(this.userId), password: '        ' }
  },
  methods: {
    // 绑定修改员工点击事件
    async updateUserInfo() {
      try {
        // 校验表单
        await this.$refs.userFrom.validate()
        // 发送请求
        await eidtEmployee(this.user)
        // 提示用户
        this.$message.success('操作成功')
        // 更新 vuex里面的数据信息
        this.$store.dispatch('user/getUserInfo')
        // 回到员工列表
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
